﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using Contracts.Caching;
using Contracts.ServiceContracts.ClientCache;
using DataService.Dataset;
using Types.Attributes;

namespace Services.BusinessObjects.ClientCache
{
	/// <summary>
	/// Used to services necessary methods to fill client cache
	/// </summary>

	[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
	internal class ClientCacheBS : BusinessBase, IClientCache
	{
		#region IClientCache Members

		public DataSet GetCacheItem(string key)
		{
			return CacheProvider.Provider.GetCacheItem<DataSet>(key);
		}

		public DataSet GetCacheItemWithRefresh(string key)
		{
			CacheProvider.Provider.RefreshCacheItem(key);
			return CacheProvider.Provider.GetCacheItem<DataSet>(key);
		}

		public void RemoveCacheItem(string key)
		{
			CacheProvider.Provider.RemoveCacheItem(key);
		}

		public DataSet GetDataForCache(string pCacheItemName)
		{
			var cacheItem = CacheProvider.ProviderSettings.CacheItems.SingleOrDefault(ci => ci.CacheItemName == pCacheItemName);

			DataSet ds = null;
			if (cacheItem.TypedDsName != null)
				ds = (DataSet)Activator.CreateInstance(cacheItem.TypedDsName);
			else
				ds = new DataSet();

			if (cacheItem == null) throw new ApplicationException("");
			if (string.IsNullOrEmpty(cacheItem.MethodName))
			{
				IDataObject dto = GetDataObject();
				string tableFullName = string.Empty;
				if (ds.Tables[0].GetType().IsDefined(typeof(FullTableNameAttribute), false))
				{
					FullTableNameAttribute dbAttribute = (FullTableNameAttribute)ds.Tables[0].GetType().GetCustomAttributes(typeof(FullTableNameAttribute), false)[0];
					tableFullName = dbAttribute.FullTableName;
				}
				SqlCommand cmd = new SqlCommand("SELECT *  FROM " + tableFullName);
				dto.GetRecords(ds.Tables[0], cmd);
			}
			else
			{
				Assembly asm = Assembly.GetAssembly(Type.GetType("Services.BusinessObjects.ClientCache.ClientCacheBS, Services"));

				foreach (Type t in asm.GetTypes())
				{
					if (t.GetInterface(cacheItem.Interfacename.FullName) != null)
					{
						object mInstance = t.InvokeMember(null, BindingFlags.CreateInstance, null, null, null);
						ds = (DataSet)cacheItem.Interfacename.GetMethod(cacheItem.MethodName).Invoke(mInstance, null);
						break;
					}
				}
			}

			return ds;
		}

		#endregion

	}
	internal class BSServiceCacheHelper : BusinessBase
	{
		public DataSet HelpTableCache(Type t)
		{
			DataSet ds = (DataSet)t.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, null, null, null);

			IDataObject dataobject = null;

			dataobject = GetDataObject();

			string tableFullName = string.Empty;

			if (ds.Tables[0].GetType().IsDefined(typeof(FullTableNameAttribute), false))
			{
				FullTableNameAttribute dbAttribute = (FullTableNameAttribute)ds.Tables[0].GetType().GetCustomAttributes(typeof(FullTableNameAttribute), false)[0];
				tableFullName = dbAttribute.FullTableName;
			}

			SqlCommand cmd = new SqlCommand("SELECT *  FROM " + tableFullName);

			dataobject.GetRecords(ds.Tables[0], cmd);

			return ds;
		}
	}





}
